package com.aynu.admin.utils.redis;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.KeyScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Component
public class RedisQuery {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public Set<String> getOnlineAccountIdList(String startsWith) {
        Cursor<byte[]> cursor = Objects.requireNonNull(stringRedisTemplate.getConnectionFactory())
                .getConnection()
                .scan(KeyScanOptions.NONE);

        Set<String> keys = new HashSet<>();
        while (cursor.hasNext()) {
            String key = new String(cursor.next());
            if(key.startsWith(startsWith)) keys.add(key.substring(startsWith.length()));
        }
        cursor.close();
        return keys;
    }
}
